﻿using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Token.Controllers {
    /// <summary>
    /// 集市商品
    /// </summary>
    [AppAuthorize("Member")]
    public class BazaarProductController : GeneralController {
        private ITokenBazaarProductService _iTokenBazaarProductService;
        private ICategoryService _iCategoryService;
        private IMerchantService _iMerchantService;
        public BazaarProductController(ITokenBazaarProductService iTokenBazaarProductService, ICategoryService iCategoryService, IMerchantService iMerchantService) {
            _iTokenBazaarProductService = iTokenBazaarProductService;
            _iCategoryService = iCategoryService;
            _iMerchantService = iMerchantService;
        }
        /// <summary>
        /// 集市首页
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult Index() {
            var categorys = _iCategoryService.GetTokenFirstDepthCatagory(1);
            ViewBag.categorys = categorys;
            var member = new merchant_dto();
            if (UserId > 0) {
                member = _iMerchantService.Get(UserId);
            }
            ViewBag.member = member;
            ViewBag.CurUserId = UserId;
            return View();
        }
        /// <summary>
        /// 首页商品
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public PartialViewResult _Index(BazaarProductQuery query) {
            ViewBag.img_url = RequestMethod.token_imgurl;
            query.is_del = false;
            query.is_all = false;
            var list = _iTokenBazaarProductService.Gets(query);
            return PartialView(list);
        }
        /// <summary>
        /// 商品详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult Detail(long id) {
            ViewBag.img_url = RequestMethod.token_imgurl;
            var result = _iTokenBazaarProductService.GetProduct(id);
            ViewBag.CurUserId = UserId;
            return View(result);
        }
        /// <summary>
        /// 商品搜索
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult Search() {
            return View();
        }
        /// <summary>
        /// 商品搜索结果页
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult SearchResult(string keyword) {
            ViewBag.keyword = keyword;
            ViewBag.img_url = RequestMethod.token_imgurl;
            return View();
        }
        /// <summary>
        /// 我发布的商品
        /// </summary>
        /// <returns></returns>
        public ActionResult Product() {
            return View();
        }
        /// <summary>
        /// 我发布的商品列表
        /// </summary>
        /// <returns></returns>
        public PartialViewResult _Product(BazaarProductQuery query) {
            ViewBag.img_url = RequestMethod.token_imgurl;
            query.merchant_id = UserId;
            query.is_all = true;
            query.is_del = false;
            var list = _iTokenBazaarProductService.Gets(query);
            return PartialView(list);
        }
        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult Delete(long id) {
            bool result = _iTokenBazaarProductService.Delete(id, UserId);
            return Json(ResultToJson.Json(result ? 1000 : 1001, result ? " 删除成功" : "删除失败", result), JsonRequestBehavior.AllowGet);
        }
    }
}